<?php

namespace app\models;

use Yii;
use app\models\Member;
use app\models\GkyUser;

/**
 * This is the model class for table "gky_project".
 *
 * @property string $id
 * @property string $projectno
 * @property string $projectname
 * @property string $userid
 * @property string $chargeman
 * @property string $unit
 * @property string $projectfrom
 * @property string $zhxtype
 * @property string $projectlevel
 * @property string $projectplantype
 * @property string $discipline
 * @property string $status
 * @property string $projectmember
 * @property integer $isfinish
 * @property string $updatetime
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * @property double $jf_dxjf
 * @property double $jf_zzjf
 * @property double $jf_jhnhx
 * @property double $jf_jhwhx
 * @property double $jf_sbgzf
 * @property double $jf_zcjf
 * @property double $jf_ppf
 * @property double $jf_zcjfhj
 * @property double $jf_syjf
 * @property string $member
 * @property string $projectmoney
 * @property string $mark
 * 
 * @property Member[] $members
 * @property Upfile[] $dupfiles
 */
class Project extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_project';
    }

    /**
     * @inheritdoc
     */
//    public function rules() {
//        return [
//            [['status','projectno','jf_dxjf', 'jf_zzjf', 'jf_jhnhx', 'jf_jhwhx', 'jf_sbgzf', 'jf_zcjf', 'jf_ppf', 'jf_zcjfhj', 'jf_syjf'], 'number'],
//            [['projectname', 'projectfrom'], 'string', 'max' => 128],
//            [['chargeman', 'unit', 'zhxtype', 'projectlevel', 'projectplantype', 'discipline', 'addman', 'projectmoney'], 'string', 'max' => 64],
//            [['projectmember', 'mark'], 'string', 'max' => 1024],
//            [['member'], 'string', 'max' => 4000],
//            //修改验证 更新时间和添加时间
//            [['updatetime', 'addtime','isfinish'], 'safe'],
//            //添加验证
//            [['projectname'], 'required'],
//        ];
//    }
    
    
     public function rules() 
    { 
        return [
            [['userid', 'addmanid'], 'integer'],
            [['updatetime', 'addtime','isfinish','projectno'], 'safe'],
            [['jf_dxjf', 'jf_zzjf', 'jf_jhnhx', 'jf_jhwhx', 'jf_sbgzf', 'jf_zcjf', 'jf_ppf', 'jf_zcjfhj', 'jf_syjf'], 'number'],
            [[ 'status'], 'string', 'max' => 32],
            [['projectname', 'projectfrom'], 'string', 'max' => 128],
            [['chargeman', 'unit', 'zhxtype', 'projectlevel', 'projectplantype', 'discipline', 'addman', 'projectmoney'], 'string', 'max' => 64],
            [['projectmember', 'mark'], 'string', 'max' => 1024],
            [['member'], 'string', 'max' => 4000],
            [['projectname'], 'required'],
        ]; 
    } 
    
    

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'projectno' => '项目编号',
            'projectname' => '项目名称',
            'userid' => '负责人编号',
            'chargeman' => '项目负责人',
            'unit' => '承担单位',
            'projectfrom' => '项目来源',
            'zhxtype' => '纵横向类型',
            'projectlevel' => '项目级别',
            'projectplantype' => '项目计划类型',
            'discipline' => '所属学科',
            'status' => '状态',
            'projectmember' => '项目成员',
            'isfinish' => '是否完成',
            'updatetime' => '更新时间',
            'addman' => '添加人',
            'addmanid' => '添加人编号',
            'addtime' => '添加时间',
            'jf_dxjf' => '到校经费',
            'jf_zzjf' => '资助经费',
            'jf_jhnhx' => '其中计划内横向经费',
            'jf_jhwhx' => '其中计划外横向经费',
            'jf_sbgzf' => '设备购置费',
            'jf_zcjf' => '自筹经费',
            'jf_ppf' => '匹配费',
            'jf_zcjfhj' => '转出经费',
            'jf_syjf' => '剩余经费',
            'member' => '成员',
            'projectmoney' => '项目经费',
            'mark' => '备注',
        ];
    }

    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);

        $upfile = new Upfile();
        // $upfile->addtime=  date("y-m-d h:i:s",time());
        $path = Yii::$app->request->post('files');
        $filename = Yii::$app->request->post('fileNames');
        $filestatus = Yii::$app->request->post('fileStatus');
        for ($i = 0; $i < count($filestatus); $i++) {
            if ($upfile->save()) {
                $upfile->activeid = $this->id;
                if ($filestatus[$i] == 1) {
                    $upfile->path = $path[$i];
                    $upfile->filename = $filename[$i];
                    $upfile->save();
                    $upfile = new Upfile();
                    //  $upfile->addtime=  date("y-m-d h:i:s",time());
                }
            }
        }
    }

    /**
     * 获取同一个项目的所有成员
     */
    public function getMembers() {
        return $this->hasMany(Member::className(), ['project_id' => 'id']);
    }

    //在这里将 本项目的所有成员保存到project表中
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            // Place your custom code here
            $membersId = Yii::$app->request->post('projectmember');
            if ($membersId != '') {
                for ($i = 0; $i < count($membersId); $i++) {
                    $members[$i] = GkyUser::find()->where(['id' => $membersId[$i]])->One()->username;
                }
                $this->projectmember = implode(',', $members);
            }
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
            $id = Yii::$app->user->identity->id;
            //添加人的id
            $this->addmanid = $id;
            //添加人的姓名
            $name = GkyUser::find()->where(['id' => $id])->one()->username;
            $this->addman = $name;
            //更新时间
            $time = date("Y-m-d");
            $this->updatetime = $time;
            //添加时间
            $this->addtime = $time;
            return true;
        } else {
            return false;
        }
    }
    
    public function getUpfiles() {

        return $this->hasMany(Upfile::className(), ['activeid' => 'id']);
    }

    public static function getList($pageNow, $pageSize){

        $list = Project::find()
            ->select(['id','projectname','chargeman','status'])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        return $list;
    }

    public static function findProjects($userid, $pageNow, $pageSize){

        $list = Project::find()
            ->select(['id','projectname','chargeman','status'])
            ->where(['userid'=>$userid])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        return $list;
    }
    public static function findPersonProjects($username, $pageNow, $pageSize){

      
         $list=  Project::find()
              ->select(['id','projectname','chargeman','status'])
              ->where(['like','chargeman',$username])
              ->orWhere(['like','member',$username])
              ->orderBy(['addtime' => SORT_DESC])
              ->offset(($pageNow - 1) * $pageSize)
              ->limit($pageSize)
              ->all();
       
        
        return $list;
    }

}
